package com.ms.p2p.controller;


import com.alibaba.dubbo.config.annotation.Reference;
import com.ms.common.bean.Message;
import com.ms.common.exception.NoticeException;
import com.ms.common.utils.AppContext;
import com.ms.common.utils.SendMail;
import com.ms.common.utils.SpringContextHolder;
import com.ms.p2p.Constants;
import com.ms.p2p.controller.BaseController;
import com.ms.p2p.entity.loan.Loan;
import com.ms.p2p.entity.loan.LoanDueIn;
import com.ms.p2p.entity.loan.LoanStay;
import com.ms.p2p.entity.member.Member;
import com.ms.p2p.provider.LoanProvider;
import com.ms.p2p.provider.LoanStayProvider;
import com.ms.p2p.provider.MemberProvider;
import com.ms.p2p.provider.SendProvider;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.freemarker.FreeMarkerTemplateUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;


/**
 * 还款
 */
@Controller
@Transactional
@RequestMapping({ "/member/repayment" })
public class LoanStayController extends BaseController {
	@Reference
	private LoanStayProvider loanStayProvider;
	@Reference
	private LoanProvider loanProvider;

	@Reference
	private MemberProvider memberProvider;

	@Reference
	private SendProvider sendProvider;
	/**
	 * 借款用户还款控制器
	 * @param session
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = { "/payloan" }, produces = { "application/json;charset=UTF-8" })
	@ResponseBody
	@Transactional
	public synchronized Message repayment(HttpSession session,
										  HttpServletRequest request, @RequestParam(value="loan_id",required = false) String loan_id){
		Member user =getCurrentUser();
		// 查询出用户要还款的标的信息
		Loan loan = loanProvider.findById(loan_id);// 还款标

		if (loan.getStatus() != 5) {
			throw  new NoticeException("该标所有还款已完成");
		}

		LoanStay loanStay = loanStayProvider
				.findNext(Integer.parseInt(loan_id));


		List<LoanDueIn> list = loanStayProvider.repay(AppContext.getRemoteIp(),loan, loanStay);


		SendRepayMail(list,loan.getName());

		return Message.success("还款成功");
	}
	@Async
	private void SendRepayMail(List<LoanDueIn> dueIns, String name) {
		FreeMarkerConfigurer FreeMarkerConfigurer = SpringContextHolder.getBean("freeMarkerConfigurer");
		String templatePath = Constants.TEMPLATE_MAIL_SUCCESS_REPAY;
		String subject = "还款";//
		Configuration configuration = FreeMarkerConfigurer.getConfiguration();
		Template template = null;
		HashMap map = new HashMap();
		map.put("loanname",name);
		String content = null;
		try {
			template = configuration.getTemplate(templatePath, "utf-8");
			for(LoanDueIn dueIn:dueIns){
				Member member = memberProvider.findById(dueIn.getDebt_user_id());
				String email = member.getEmail();
				if(email==null){
					continue;
				}
				map.put("username",member.getUser_name());
				map.put("stay_order",dueIn.getDuein_periods());
				map.put("capital",dueIn.getDuein_capital());
				map.put("interest",dueIn.getDuein_interest());
				content = FreeMarkerTemplateUtils.processTemplateIntoString(template, map);
				sendProvider.sendMail(email,subject,content);
			}
		} catch (IOException e) {
			e.printStackTrace();
		} catch (TemplateException e) {
			e.printStackTrace();
		}


	}



}
